package test.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import junit.framework.TestCase;
import test.service.BaseTest;
import blog.core.exception.FormParamErrorException;
import blog.core.exception.NotFoundException;
import blog.spring.db.entity.ArticleInfo;
import blog.spring.db.entity.Type;
import blog.spring.db.service.ITypeService;

public class TypeService extends BaseTest<Type> {

	@Resource
	private ITypeService typeService;

	@Override
	public void init() {
		entity = new Type();
		entity.setTitle("我是标题");
		entity.setDescribeType(1);
	}

	@Override
	public void a_save() throws FormParamErrorException {
		typeService.save(entity);
	}

	@Override
	public void b_findByName() {
		TestCase.assertNotNull(typeService.findByName(entity.getTitle()));
	}

	@Override
	public void c_findById() throws NotFoundException {
//		TestCase.assertNotNull(typeService.findById(typeService.findByName(
//				entity.getTitle()).getTypeId()));
		for (ArticleInfo info : typeService.findById(20).getArticleInfos()) {
			System.out.println(info.getTitle());
		}
	}

	@Override
	public void d_update() throws FormParamErrorException, NotFoundException {
		Type t = typeService.findByName(entity.getTitle());
		t.setTitle("我是修改后的标题");
		typeService.update(t);
	}

	@Override
	public void e_remove() {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("title", "标题");
		typeService.remove(typeService.searchUnique(conditions, true));
	}

	@Override
	public void f_addMulti() throws FormParamErrorException {
		for (int i = 0; i < 100; i++) {
			entity = new Type();
			entity.setTitle("标题" + i);
			typeService.save(entity);
		}
	}

	@Override
	public void g_findList() {
		TestCase.assertTrue(typeService.findList(1, 10, null, null).size() > 0);
	}

	@Override
	public void h_searchList() {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("title", "标题");
		TestCase.assertTrue(typeService.searchList(conditions, 1, 10, null,
				null, true).size() > 0);
	}

	@Override
	public void i_searchUnique() {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("title", "标题");
		TestCase.assertNotNull(typeService.searchUnique(conditions, true));
	}

}
